﻿using System;
using System.Collections.Generic;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Model;
using System.Data;
using IntelligentStorWpfApp.Services;
using System.Windows;

namespace IntelligentStorWpfApp.Controller
{
    public class PositionController
    {
        PositionInterface pi = new PositionServices();
        StatusController statusControl = new StatusController();
        OperationLogController opControl = new OperationLogController();
        /// <summary>
        /// 新增箱柜
        /// </summary>
        /// <returns></returns>
        public bool addPosition(Position position)
        {
            bool result = false;
            try
            {
                result = pi.Add(position);
                if(result)
                {
                    DataTable dt= pi.getSinglePosition(position);
                    Status status = new Status();
                    status.PositionId =Convert.ToInt32(dt.Rows[0]["ID"]);
                    status.StoreStatus = 0;

                    if(! statusControl.AddStatus(status))
                    {
                        pi.Delete(status.PositionId);
                        result = false;
                    }
                    else
                    {
                        opControl.AddLog(new OperationLog() { UserName = App.user.UserName, OperatTime = DateTime.Now, Actions = "新增"+position.CabinetNum.ToString()+"柜"+position.PositionNum+"号" });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据柜号获取所有箱格
        /// </summary>
        /// <param name="cabinetNum"></param>
        /// <returns></returns>
        public List<Position> getPositions(int cabinetNum)
        {
            Position postion = new Position();
            postion.CabinetNum = cabinetNum;
            DataTable dt = pi.GetPositions(postion);
            List<Position> listPosition = new List<Position>();
            for(int i=0;i<dt.Rows.Count;i++)
            {
                Position p = new Position();
                p.Id = Convert.ToInt32( dt.Rows[i]["ID"]);
                p.PositionNum = Convert.ToInt32(dt.Rows[i]["PositionNum"]);
                if (dt.Rows[i]["StoreStatus"] != null)
                    p.Status = Convert.ToInt32(dt.Rows[i]["StoreStatus"]);
                else
                    p.Status = 0;

                listPosition.Add(p);
            }
            return listPosition;
        }
        /// <summary>
        /// 获取所有柜号
        /// </summary>
        /// <returns></returns>
        public DataTable getCabinetNums()
        {
            return pi.GetCabinetNums();
        }

        public int getPostionId(Position position)
        {
            Position p = new Position();
            p.Id = -1;
            DataTable dt = pi.getSinglePosition(position);
            if(dt.Rows.Count>0)
            {
                p.Id = Convert.ToInt32(dt.Rows[0]["Id"]);
            }
            return p.Id;
        }
        
    }
}
